<template>
	<view class="conter">
		<view class="headnavs">
			<view class="iacon">
				<u-icon color="#fff" bold size="20" @click="handleGoBack" class="left" name="arrow-left"></u-icon>
			</view>
			<view class="anchor">上任详情</view>
			<view @click="goxiangqing" class="imagebox">
				<image src="../../static/images/crazydetalii.png" mode=""></image>
			</view>
		</view>
		<!-- 导航栏 -->
		<u-list height="1020rpx" @scrolltolower="scrolltolower">
			<view v-if="huntIngList.length != 0">
				<view class="list_duck">
					<u-list-item v-for="(item, index) in huntIngList" :key="index">
						<view @click="clickMon(item, index)">
							<view :class="currentIndex === index ? 'min_ducklist1' : 'min_ducklist'">
								<view class="duck_image">
									<!-- 背景图 -->
									<image :src="imgUrl + item.collectionUrl" mode="" class="imgsos"></image>
									<view class="number_duck">#{{ item.petId }}</view>
								</view>
								<view class="duck_buttom">
									<view class="name_duck">
										<view class="name">Duck</view>
										<view class="num">繁育次数：{{ item.birthNumber }}/5</view>
									</view>
									<view class="expedition">上任中</view>
								</view>
							</view>
						</view>
					</u-list-item>
				</view>
			</view>
			<view class="bookstore" v-if="huntIngList.length == 0">
				<image style="
				width: 280rpx;
				height: 280rpx;
				margin: 105rpx 70rpx 0;
				" src="../../static/images/fighting.gif" mode=""></image>
			</view>
		</u-list>
		<view class="qiehaun">
			<view class="">
				<image :src="weatherImage" mode=""></image>
			</view>
			<view class="count_time">
				<u-count-down :time="checkBabyMonDeail.time" format="HH:mm:ss"></u-count-down>
			</view>
			<view class="images">
				<image src="../../static/images/crazy/jinbi.png" mode=""></image>
				<view class="txt">{{ checkBabyMonDeail.reward }}枚</view>
			</view>

			<view class="progress">
				<u-line-progress :percentage="percentageTime" activeColor="#46a6f8"></u-line-progress>
			</view>
			<view v-show="isGetTrue" class="buttom_box">
				<view @click="toTakeHunted" class="crazyFiveSplit">全部领取</view>
			</view>
			<view @click="gobuck" class="gobuck">去上任</view>
		</view>
	</view>
</template>

<script>
	import {
		pocoHunting,
		getHuntingList,
		takeHunted,
		drugPocoHunting
	} from '@/api/breed/crazy.js';
	import config from '@/config';
	import {
		toast
	} from '../../utils/common';
	export default {
		data() {
			return {
				level: '',
				total: 0,
				param: {
					page: 1,
					size: 10
				},
				imgUrl: config.gillUrl,
				imgUrl: config.imgUrl,
				isGetTrue: false, //是否有待领取的，为true则需要显示领取按钮
				loading: false,
				percentageTime: '', //时间
				checkboxValue1: [],
				huntIngList: [], //正在狩猎的bbmon列表
				//选中狩猎中的bbmon
				checkBabyMonDeail: {
					collectionUrl: '', //宝可图片
					weather: '', // 天气
					weatherImg: '', //天气图片
					crazyTime: 0, //疯狂时间
					expirationTime: '', //结束时间，领取时间
					isGet: 1, //是否领取奖励(1探险中，2待领取，3已领取)
					reward: 0, //奖励的时之沙数量
					time: 0 //倒计时
				},
				currentIndex: -1, // 记录当前点击的项的索引
				receivedItems: []
			};
		},
		computed: {
			weatherImage() {
				let weatherImage;
				switch (this.checkBabyMonDeail.weather) {
					case '船员鸭':
						weatherImage = 'crew.png';
						break;
					case '主播鸭':
						weatherImage = 'zhubo.png';
						break;
					case '大厨鸭':
						weatherImage = 'chef.png';
						break;
					case '骑手鸭':
						weatherImage = 'rider.png';
						break;
					case '保安鸭':
						weatherImage = 'security_staff.png';
						break;
					case '技师鸭':
						weatherImage = 'massage.png';
						break;
					case '乞丐鸭':
						weatherImage = 'beggar.png';
						break;
					case '主持鸭':
						weatherImage = 'zhuchi.png';
						break;
					case '程序鸭':
						weatherImage = 'cheng.png';
						break;
					case '金融鸭':
						weatherImage = 'jin.png';
						break;
					default:
						weatherImage = 'gogoduck_info.png'; // 默认
				}

				return this.imgUrl + `/static/images/crazy/${weatherImage}`;
			}
		},
		onLoad(query) {
			// 获取路由参数并转为数组
			this.receivedItems = JSON.parse(query.selectedItems || '[]');
			this.level = query.level;
			this.extractedIds = this.receivedItems.map((item) => item.id);
			if (this.extractedIds.length) {
				this.onAdd();
			}
			// this.getHuntingIngList();
			// 在数据加载完成后，隐藏加载页
		},
		created() {
			this.getHuntingIngList();
		},
		methods: {
			goxiangqing() {
				uni.navigateTo({
					url: '/pages/breeding/breedingxiang'
				})
			},
			handleGoBack() {
				uni.switchTab({
					url: '/pages/work/duck'
				});
			},
			// 添加状态
			async onAdd() {
				let res = await pocoHunting(this.extractedIds);
				if (res.code == 200) {
					toast(res.data);
					let _this = this;
					setTimeout(function() {
						_this.checkboxValue1 = [];
						_this.show = false;
						_this.huntIngList = []
						_this.getHuntingIngList();
					}, 700);
				}
			},
			//获取正在狩猎的bbmon列表
			async getHuntingIngList() {
				let res = await getHuntingList(this.param);
				if (res.code == 200) {
					// 在请求新数据之前清理旧数据
					let _this = this;
					res.data.pageData.forEach((item) => {
						_this.huntIngList.push(item);
					});
					this.total = res.data.totalSize;
				}
				// 如果有数据则选择第一条数据
				if (this.huntIngList.length > 0) {
					let one = this.huntIngList[0];
					this.checkBabyMonDeail.isGet = one.isGet;
					this.checkBabyMonDeail.reward = one.reward;
					this.checkBabyMonDeail.weather = one.weather;
					//要计算倒计时
					let time = this.countdown(one.expirationTime, one.crazyTime);
					this.checkBabyMonDeail.time = one.countDown;
					this.percentageTime = Math.floor((this.checkBabyMonDeail.time / 1000 / 60 / 480) * 100);
					// console.log(this.percentageTime);
				} else {
					this.checkBabyMonDeail = this.checkBabyMonDeail1;
					this.isGetTrue = false;
				}
				this.huntIngList.map((i) => {
					if (i.isGet == 2) {
						this.isGetTrue = true;
					}
				});
			},
			scrolltolower() {
				//加载时候判断是否还有下一页
				if (this.huntIngList.length < this.total) {
					this.param.page++;
					//则还有下一页
					this.status = 'loadmore';
					this.getHuntingIngList();
				} else {
					this.param.page = 1;
					this.status = 'nomore';
				}
			},
			//选择mon
			clickMon(item, index) {
				this.loading = true;
				this.currentIndex = index;
				this.checkBabyMonDeail.weather = item.weather;
				this.checkBabyMonDeail.isGet = item.isGet; //判断是否改领取
				this.checkBabyMonDeail.reward = item.reward; //领取数量
				//计算倒计时
				let time = this.countdown(item.expirationTime, item.crazyTime);
				this.checkBabyMonDeail.time = item.countDown;
				if (this.checkBabyMonDeail.time > 28800000) {
					this.percentageTime = Math.floor((this.checkBabyMonDeail.time / 1000 / 60 / 4320) * 100);
				} else {
					this.percentageTime = Math.floor((this.checkBabyMonDeail.time / 1000 / 60 / 480) * 100);
				}
				this.loading = false;
			},
			// 一键领取所有
			async toTakeHunted() {
				let res = await takeHunted();
				if (res.code == 200) {
					toast('领取成功');
					this.isGetTrue = false;
					this.huntIngList = []; //领取成功后清空数组
					this.page = 1; // 重置页码至第一页以重新获取整个列表
					await this.getHuntingIngList(); // 领取成功后立即获取新数据
				}
			},
			//计算倒计时
			countdown(time, crazyTime) {
				let endTime = new Date(time);
				// let beginTime = new Date(endTime.setHours(endTime.getHours() - crazyTime));
				let curTime = new Date();
				let lastTime = endTime.getTime() - curTime.getTime();
				// console.log(lastTime, 'ppppp');
				return lastTime;
			},
			gobuck() {
				uni.navigateTo({
					url: '/pages/breeding/Crazy'
				});
			}
		}
	};
</script>
<style lang="scss" scoped>
	::v-deep .u-count-down__text {
		color: #fff;
		font-size: 32rpx !important;
		font-weight: 500;
		line-height: 72rpx !important;
	}

	.conter {
		padding: 60rpx 0 0;

		.count_time {
			top: 30rpx;
			left: 34.88rpx;
			width: 197rpx;
			height: 72rpx;
			position: absolute;
			text-align: center;
			border-radius: 36rpx;
			background: rgba(0, 0, 0, 0.7);
		}

		.bookstore {
			top: 250rpx;
			left: 180rpx;
			position: absolute;
		}

		.headnavs {
			width: 750rpx;
			height: 112rpx;
			display: flex;
			align-items: center;
			padding: 0 40rpx 0 40rpx;
			justify-content: space-between;
			background: #1c213a;

			.iacon {
				left: 34.88rpx;
			}

			.anchor {
				color: #ffffff;
				font-size: 35rpx;
				font-family: 'font';
				font-weight: 800;
			}

			.imagebox {
				margin-top: 12rpx;

				image {
					width: 50rpx;
					height: 50rpx;
				}
			}
		}

		.qiehaun {
			width: 680rpx;
			height: 350rpx;
			margin-top: 70rpx;
			border-radius: 14rpx;
			margin: 50rpx 34.88rpx 0;
			position: relative;

			image {
				width: 680rpx;
				height: 350rpx;
			}

			.images {
				width: 197rpx;
				height: 72rpx;
				line-height: 72rpx;
				background: rgba(0, 0, 0, 0.7);
				border-radius: 36rpx 36rpx 36rpx 36rpx;
				top: 30rpx;
				right: 37rpx;
				position: absolute;
				display: flex;
				color: #ffffff;

				image {
					width: 47rpx;
					height: 49rpx;
					border-radius: 0rpx 0rpx 0rpx 0rpx;
					opacity: 1;
					margin-top: 10rpx;
					margin-left: 29rpx;
				}

				.txt {
					margin-left: 15rpx;
				}
			}

			.progress {
				width: 90%;
				margin-left: 5%;
				margin-top: -60rpx;
			}

			.buttom_box {
				position: absolute;
				top: 120rpx;
				right: 45rpx;

				.crazyFiveSplit {
					color: #fff;
					font-weight: 500;
					font-family: 'font';
					font-size: 24rpx;
					padding: 15rpx 45rpx 15rpx 45rpx;
					border-radius: 14rpx;
					background: linear-gradient(90deg, #3adfeb 0%, #4463ff 100%);
					box-shadow: 3rpx 3rpx 5rpx 0rpx rgba(0, 0, 0, 0.15);
				}
			}

			.gobuck {
				color: #fff;
				bottom: 80rpx;
				left: 34.88rpx;
				font-weight: 800;
				font-size: 24rpx;
				font-family: 'font';
				position: absolute;
				border-radius: 14rpx;
				padding: 15rpx 45rpx 15rpx 45rpx;
				background: linear-gradient(90deg, #3adfeb 0%, #4463ff 100%);
				box-shadow: 3rpx 3rpx 5rpx 0rpx rgba(0, 0, 0, 0.15);
			}
		}

		.list_duck {
			width: 680rpx;
			display: flex;
			flex-wrap: wrap;
			margin: 0 34.88rpx;
			justify-content: space-between;

			.min_ducklist1 {
				width: 324rpx;
				height: 433rpx;
				border-radius: 14rpx;
				border: 5rpx solid #46a6f8;
			}

			.min_ducklist {
				width: 318rpx;
				height: 408rpx;
				margin-bottom: 45.35rpx;
			}

			.duck_image {
				position: relative;
				width: 316rpx;
				height: 316rpx;
				border-top: 1rpx solid #3c424d;
				border-left: 1rpx solid #3c424d;
				border-right: 1rpx solid #3c424d;
				border-radius: 9rpx 9rpx 0rpx 0rpx;

				image {
					width: 316rpx;
					height: 316rpx;
					background: #648bd8;
					border-radius: 9rpx 9rpx 0rpx 0rpx;
				}

				.lock {
					width: 316rpx;
					height: 316rpx;
					border-radius: 9rpx 9rpx 0rpx 0rpx;
					position: absolute;
					z-index: 100;
					left: 0;
				}
			}

			.number_duck {
				top: 17.44rpx;
				left: 17.44rpx;
				background: rgba(40, 44, 52, 0.8);
				border-radius: 17rpx;
				position: absolute;
				padding: 5rpx 10rpx 2rpx 10rpx;
				font-family: 'font';
				font-weight: 500;
				color: #46a6f8;
				font-size: 24rpx;
			}

			.duck_buttom {
				width: 316rpx;
				height: 110rpx;
				border-bottom: 1rpx solid #3c424d;
				border-left: 1rpx solid #3c424d;
				border-right: 1rpx solid #3c424d;
				background: #211f30;
				display: flex;
				justify-content: space-between;
				padding: 0 10rpx;
				border-radius: 0rpx 0rpx 9rpx 9rpx;

				.name_duck {
					.name {
						font-size: 24rpx;
						font-family: 'font';
						font-weight: 500;
						color: #ffffff;
						margin: 18rpx 0 0 17rpx;
					}

					.num {
						font-size: 20rpx;
						font-family: 'font';
						font-weight: 500;
						color: #8a92a3;
						margin: 15rpx 0 5rpx 17rpx;
					}
				}

				.expedition {
					width: 90rpx;
					height: 35rpx;
					color: #ffffff;
					font-size: 22rpx;
					line-height: 38rpx;
					font-weight: 500;
					margin-top: 55rpx;
					font-family: 'font';
					text-align: center;
					background: #46a6f8;
					border-radius: 9rpx;
				}
			}
		}
	}
</style>